package com.haierp.controller.purchase;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.model.purchase.PurchaseAssistTime;
import com.haierp.service.purchase.IPurchaseAssistTimeService;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonResult;
import com.wangzhixuan.commons.base.BaseController;

/**
 *
 * PurchaseAssistTime 控制层
 *
 */
@Controller
@RequestMapping("/assistPurchase")
public class PurchaseAssistTimeController extends BaseController{

	@Autowired
	IPurchaseAssistTimeService purchaseAssistTimeService;

	@RequestMapping("/editAssistPurchase")
	@ResponseBody
	public Object editAssistPurchase(PurchaseAssistTime purchaseAssistTime) {
		JsonResult<String> result = new JsonResult<>();
		if(purchaseAssistTime.getId()!=null) {
			purchaseAssistTime.setGmtModify(new Date());
			purchaseAssistTimeService.updateSelectiveById(purchaseAssistTime);
		} else {
			purchaseAssistTime.setGmtCreate(new Date());
			purchaseAssistTime.setGmtModify(new Date());
			purchaseAssistTimeService.insert(purchaseAssistTime);
		}
		return result.buildIsSuccess(true).buildMsg("添加成功");
	}

	@RequestMapping("/queryAssistPurchase")
	public Object queryAssistPurchase(HttpServletRequest request) {
		EntityWrapper<PurchaseAssistTime> entityWrapper = new EntityWrapper<>();
		entityWrapper.orderBy("gmt_create", false);
		List<PurchaseAssistTime> purchaseAssistTimeList = purchaseAssistTimeService.selectList(entityWrapper);
		Map<String,PurchaseAssistTime> timeMap = new HashMap<String,PurchaseAssistTime>();
		for (PurchaseAssistTime purchaseAssistTime : purchaseAssistTimeList) {
			purchaseAssistTime.setDimensionDate(DateUtil.formatSimpleDate(purchaseAssistTime.getGmtCreate()));
			if(timeMap.containsKey(purchaseAssistTime.getDimensionDate())) {
				PurchaseAssistTime purchaseAssistTimeNew = timeMap.get(purchaseAssistTime.getDimensionDate());
				purchaseAssistTimeNew.setCount(purchaseAssistTimeNew.getCount()+1);
			} else {
				PurchaseAssistTime purchaseAssistTimeNew = new PurchaseAssistTime();
				purchaseAssistTimeNew.setCount(1L);
				timeMap.put(purchaseAssistTime.getDimensionDate(), purchaseAssistTimeNew);
			}
		}
		request.setAttribute("timeMap", timeMap);
		request.setAttribute("purchaseAssistTimeList", purchaseAssistTimeList);
		return "kpi/assistPurchase";
	}
}